<template>
  <!-- 气泡背景 -->
  <div class="bruce">
    <ul class="bubble-bgwall">
      <li>RAG</li>
      <li>AI</li>
      <li>Register</li>
      <li>System</li>
      <li>Welcome</li>
      <li>Safe</li>
      <li>Modern</li>
      <li>Fast</li>
      <li>Smart</li>
      <li>Cloud</li>
    </ul>
  </div>

  <!-- 注册容器 -->
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <h1>RAG AI系统</h1>
        <p class="subtitle">智能问答与文档管理系统</p>
      </div>

      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名 <span class="required">*</span></label>
          <input 
            id="username" 
            v-model="registerForm.username" 
            type="text" 
            placeholder="请输入用户名"
            required 
          />
        </div>

        <div class="form-group">
          <label for="password">密码 <span class="required">*</span></label>
          <div class="password-container">
            <input 
              id="password" 
              v-model="registerForm.password" 
              :type="showPassword ? 'text' : 'password'" 
              placeholder="请输入密码"
              required 
            />
            <button 
              type="button" 
              class="eye-button" 
              @click="showPassword = !showPassword"
            >
              <span v-if="showPassword">👁️</span>
              <span v-else>👁️‍🗨️</span>
            </button>
          </div>
        </div>

        <div class="form-group">
          <label for="confirmPassword">确认密码 <span class="required">*</span></label>
          <div class="password-container">
            <input 
              id="confirmPassword" 
              v-model="confirmPassword" 
              :type="showConfirmPassword ? 'text' : 'password'" 
              placeholder="请再次输入密码"
              required 
            />
            <button 
              type="button" 
              class="eye-button" 
              @click="showConfirmPassword = !showConfirmPassword"
            >
              <span v-if="showConfirmPassword">👁️</span>
              <span v-else>👁️‍🗨️</span>
            </button>
          </div>
        </div>

        <div class="button-container">
          <button type="submit" class="register-button" :disabled="loading">
            <span v-if="!loading">注册</span>
            <span v-else>注册中...</span>
          </button>
          <button type="button" @click="goToLogin" class="login-link-button">
            返回登录
          </button>
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const registerForm = ref({
  username: '',
  password: ''
})

const confirmPassword = ref('')
const loading = ref(false)
const error = ref('')
const showPassword = ref(false)
const showConfirmPassword = ref(false)

const handleRegister = async () => {
  try {
    error.value = ''

    // 基本验证
    if (!registerForm.value.username) {
      error.value = '请输入用户名'
      return
    }
    if (registerForm.value.username.length < 3) {
      error.value = '用户名至少3个字符'
      return
    }
    if (!registerForm.value.password) {
      error.value = '请输入密码'
      return
    }
    if (registerForm.value.password.length < 6) {
      error.value = '密码长度至少6位'
      return
    }
    if (!confirmPassword.value) {
      error.value = '请确认密码'
      return
    }
    if (registerForm.value.password !== confirmPassword.value) {
      error.value = '两次输入密码不一致'
      return
    }

    loading.value = true
    
    // 构建完整的注册数据，确保所有字段都存在
    const registerData = {
      username: registerForm.value.username,
      password: registerForm.value.password,
      email: '',
      phoneNumber: '',
      role: 'user'
    }
    
    await userStore.register(registerData)
    
    ElMessage.success('注册成功，请登录')
    setTimeout(() => {
      router.push('/login')
    }, 1500)
  } catch (err) {
    console.error('Register error:', err)
    error.value = err.message || '注册失败'
    ElMessage.error(error.value)
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 气泡背景样式 */
.bruce {
  height: 100vh;
  width: 100vw;
  background-image: linear-gradient(270deg, #4facfe, #00f2fe);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  overflow: hidden;
}

.bubble-bgwall {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  list-style: none;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  font-size: 14px;
  font-weight: 500;
  animation: bubble 15s infinite;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 160px;
  height: 160px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }
  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }
  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }
  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}

/* 注册容器 */
.register-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  z-index: 1000;
  animation: fadeIn 0.6s ease;
}

/* 注册框 */
.register-box {
  background: white;
  border-radius: 15px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  width: 100%;
  overflow: hidden;
  backdrop-filter: blur(10px);
}

/* 注册头部 */
.register-header {
  padding: 35px 30px 25px;
  text-align: center;
  background: linear-gradient(135deg, #4facfe, #00f2fe);
  color: white;
  position: relative;
  overflow: hidden;
}

.register-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
  pointer-events: none;
}

.register-header h1 {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 8px;
  position: relative;
  z-index: 1;
}

.register-header .subtitle {
  font-size: 14px;
  opacity: 0.9;
  position: relative;
  z-index: 1;
}

/* 注册表单 */
.register-form {
  padding: 30px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.required {
  color: #ff4757;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e1e8ed;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-group input:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 2px rgba(79, 172, 254, 0.2);
}

.form-group input:hover {
  border-color: #4facfe;
  box-shadow: 0 2px 12px rgba(79, 172, 254, 0.2);
}

.form-group input.error {
  border-color: #ff4757;
  box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.2);
}

.password-container {
  position: relative;
  display: flex;
  gap: 10px;
}

/* 密码眼睛图标 */
.eye-button {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
  padding: 4px;
  font-size: 16px;
  transition: color 0.3s ease;
}

.eye-button:hover {
  color: #4facfe;
}

/* 按钮容器 */
.button-container {
  margin-top: 30px;
}

/* 注册按钮 */
.register-button {
  width: 100%;
  padding: 14px 20px;
  background: linear-gradient(135deg, #4facfe, #00f2fe);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(79, 172, 254, 0.3);
  margin-bottom: 15px;
}

.register-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #00f2fe, #4facfe);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(79, 172, 254, 0.4);
}

.register-button:disabled {
  opacity: 0.8;
  cursor: not-allowed;
  transform: none;
}

/* 登录链接按钮 */
.login-link-button {
  width: 100%;
  padding: 14px 20px;
  background: white;
  color: #4facfe;
  border: 2px solid #4facfe;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-link-button:hover {
  background: #4facfe;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(79, 172, 254, 0.3);
}

/* 错误消息 */
.error-message {
  margin-top: 20px;
  padding: 12px 16px;
  background: linear-gradient(135deg, #ffebee, #ffcdd2);
  color: #c62828;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  border: 1px solid #ef9a9a;
  animation: shake 0.5s ease;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

@keyframes gentleShake {
  0%, 100% { transform: translateY(0); }
  25% { transform: translateY(-1px); }
  75% { transform: translateY(1px); }
}

@keyframes pulse {
  0%, 100% { 
    transform: scale(1);
    opacity: 1;
  }
  50% { 
    transform: scale(1.1);
    opacity: 0.8;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    width: 95vw;
    max-height: 95vh;
  }
  
  .register-header {
    padding: 25px 20px 20px;
  }
  
  .register-form {
    padding: 20px;
  }
  
  /* 未使用 - 验证码响应式 */
  /*
  .code-container,
  .captcha-container {
    flex-direction: column;
  }
  
  .code-button {
    min-width: auto;
  }
  
  .captcha-wrapper {
    width: 100%;
    margin-top: 10px;
  }
  
  .agreement-section {
    padding: 2px;
  }
  
  .agreement-box {
    padding: 12px;
  }
  
  .agreement-error-tip {
    padding: 8px 10px;
    font-size: 12px;
  }
  */
}

@media (max-width: 480px) {
  .bubble-bgwall li {
    display: none;
  }
  
  .bubble-bgwall li:nth-child(1),
  .bubble-bgwall li:nth-child(3),
  .bubble-bgwall li:nth-child(5) {
    display: flex;
  }
  
  .register-container {
    width: 98vw;
    max-height: 98vh;
  }
}

/* 滚动条样式 */
.register-container::-webkit-scrollbar {
  width: 6px;
}

.register-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
}

.register-container::-webkit-scrollbar-thumb {
  background: rgba(79, 172, 254, 0.5);
  border-radius: 3px;
}

.register-container::-webkit-scrollbar-thumb:hover {
  background: rgba(79, 172, 254, 0.7);
}
</style>
